package com.thingple.nfc;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.NfcA;
import android.nfc.tech.NfcV;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;

import com.thingple.util.Alert;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class NfcActivity extends AppCompatActivity {

    private static final String TAG = NfcActivity.class.getName();

    private NfcAdapter nfcAdapter;
    private PendingIntent pi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfc);
        Alert.ctx = this;
        init();
    }

    void init() {
        Log.d(TAG, "init nfc adapter...");
        Alert.msg("init nfc adapter...");
        nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        Intent intent = new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "nfc activity resume");
        nfcAdapter.enableForegroundDispatch(this, pi, null, null);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d(TAG, "new nfc tag in...");
        nfcHandler(intent);
    }

    private void nfcHandler(Intent intent) {
        String action = intent.getAction();
        if (action == null) {
            return;
        }
        switch (action) {
            case NfcAdapter.ACTION_NDEF_DISCOVERED:
                getNdef(intent);
                break;
            case NfcAdapter.ACTION_TECH_DISCOVERED:
                processTech(intent);
                break;
            case NfcAdapter.ACTION_TAG_DISCOVERED:
                processTag(intent);
                break;
            default:
                Log.d(TAG, "unknown tag");
        }


    }

    private NdefMessageWrapper getNdef(Intent intent) {
        Alert.msg("NDEF tag...");
        NdefMessageWrapper wrapper = new NdefMessageWrapper(intent);
        return wrapper;
    }

    private void processTech(Intent intent) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (tag == null) {
            return;
        }
        boolean ok = false;
        ok = processNfcA(tag);
        if (ok) {
            return;
        }
    }

    private boolean processNfcA(Tag tag) { // ISO14443A
        NfcA n = NfcA.get(tag);
        if (n == null) {
            return false;
        }
        Alert.msg("NFC_A card...");
        try {
            NfcAHandler handler = new NfcAHandler(n);
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    private boolean processNfcV(Tag tag) { // ISO15693
        NfcV nv = NfcV.get(tag);
        if (nv == null) {
            return false;
        }
        try {
            NfcVHandler handler = new NfcVHandler(nv);
            Log.d(TAG, "nfc_v:" + handler.uid);
            Alert.msg("nfc_v(15693):" + handler.uid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            nv.isConnected();
        }
        return true;
    }

    private void processTag(Intent intent) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        byte[] id = tag.getId();
        boolean ok = false;
        ok = processNfcV(tag);
        if (ok) {
            return;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        nfcAdapter = null;
    }
}